package com.kaoyan.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaoyan.model.*;
import com.kaoyan.service.KaoyanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

@Controller
public class KaoyanController {
    @Autowired
    private KaoyanService kaoyanService;

    @GetMapping("/kaoyan")
    public ModelAndView getKaoyanData() {
        List<AdjustmentData> adjustmentData = kaoyanService.getAdjustmentData();
        List<ScoreLine> scoreLine = kaoyanService.getScoreLine();
        List<SchoolAdjustmentCounts> schoolAdjustmentCounts = kaoyanService.getSchoolAdjustmentCounts();
        List<AdjustmentTimeDistribution> adjustmentTimeDistribution = kaoyanService.getAdjustmentTimeDistribution();
        List<YearlyExamineeCounts> yearlyExamineeCounts = kaoyanService.getYearlyExamineeCounts();
        List<SubjectScoreDistribution> subjectScoreDistribution = kaoyanService.getSubjectScoreDistribution();
        List<AdjustmentScoreRelationship> adjustmentScoreRelationship = kaoyanService.getAdjustmentScoreRelationship();
        List<YearlyAdjustmentScoreTrend> yearlyAdjustmentScoreTrend = kaoyanService.getYearlyAdjustmentScoreTrend();
        List<MonthlyAdjustmentFrequency> monthlyAdjustmentFrequency = kaoyanService.getMonthlyAdjustmentFrequency();
        List<SubjectScoresViolin> subjectScoresViolin = kaoyanService.getSubjectScoresViolin();
        List<TopSchoolsScoreRadar> topSchoolsScoreRadar = kaoyanService.getTopSchoolsScoreRadar();
        List<YearlySchoolAdjustmentScore> yearlySchoolAdjustmentScore = kaoyanService.getYearlySchoolAdjustmentScore();

        ModelAndView view = new ModelAndView("detail");
        view.addObject("adjustmentData", adjustmentData);
        view.addObject("scoreLine", scoreLine);
        view.addObject("schoolAdjustmentCounts", schoolAdjustmentCounts);
        view.addObject("adjustmentTimeDistribution", adjustmentTimeDistribution);
        view.addObject("yearlyExamineeCounts", yearlyExamineeCounts);
        view.addObject("subjectScoreDistribution", subjectScoreDistribution);
        view.addObject("adjustmentScoreRelationship", adjustmentScoreRelationship);
        view.addObject("yearlyAdjustmentScoreTrend", yearlyAdjustmentScoreTrend);
        view.addObject("monthlyAdjustmentFrequency", monthlyAdjustmentFrequency);
        view.addObject("subjectScoresViolin", subjectScoresViolin);
        view.addObject("topSchoolsScoreRadar", topSchoolsScoreRadar);
        view.addObject("yearlySchoolAdjustmentScore", yearlySchoolAdjustmentScore);

        return view;
    }

    @GetMapping("/charts")
    public ModelAndView showChartsPage() throws IOException {
        ModelAndView view = new ModelAndView("charts");
        ObjectMapper objectMapper = new ObjectMapper(); // Jackson对象映射器

        // 1. 学校调剂数量柱状图数据
        List<SchoolAdjustmentCounts> schoolAdjustmentCounts = kaoyanService.getSchoolAdjustmentCounts();
        view.addObject("schoolAdjustmentCountsJson", objectMapper.writeValueAsString(schoolAdjustmentCounts));

        // 2. 调剂时间分布折线图数据
        List<AdjustmentTimeDistribution> adjustmentTimeDistribution = kaoyanService.getAdjustmentTimeDistribution();
        view.addObject("adjustmentTimeDistributionJson", objectMapper.writeValueAsString(adjustmentTimeDistribution));

        // 3. 不同年份考研人数饼图数据
        List<YearlyExamineeCounts> yearlyExamineeCounts = kaoyanService.getYearlyExamineeCounts();
        view.addObject("yearlyExamineeCountsJson", objectMapper.writeValueAsString(yearlyExamineeCounts));

        // 4. 各学科总分箱线图数据
        List<SubjectScoreDistribution> subjectScoreDistribution = kaoyanService.getSubjectScoreDistribution();
        view.addObject("subjectScoreDistributionJson", objectMapper.writeValueAsString(subjectScoreDistribution));

        // 5. 调剂学校与分数线关联散点图数据
        List<AdjustmentScoreRelationship> adjustmentScoreRelationship = kaoyanService.getAdjustmentScoreRelationship();
        view.addObject("adjustmentScoreRelationshipJson", objectMapper.writeValueAsString(adjustmentScoreRelationship));

        // 6. 趋势对比图数据（调剂数量与分数线）
        List<YearlyAdjustmentScoreTrend> yearlyAdjustmentScoreTrend = kaoyanService.getYearlyAdjustmentScoreTrend();
        view.addObject("yearlyAdjustmentScoreTrendJson", objectMapper.writeValueAsString(yearlyAdjustmentScoreTrend));

        // 7. 每月调剂信息发布频率热力图数据
        List<MonthlyAdjustmentFrequency> monthlyAdjustmentFrequency = kaoyanService.getMonthlyAdjustmentFrequency();
        view.addObject("monthlyAdjustmentFrequencyJson", objectMapper.writeValueAsString(monthlyAdjustmentFrequency));

        // 8. 各学科各科目分数小提琴图数据
        List<SubjectScoresViolin> subjectScoresViolin = kaoyanService.getSubjectScoresViolin();
        view.addObject("subjectScoresViolinJson", objectMapper.writeValueAsString(subjectScoresViolin));

        // 9. 热门学校分数线分布雷达图数据
        List<TopSchoolsScoreRadar> topSchoolsScoreRadar = kaoyanService.getTopSchoolsScoreRadar();
        view.addObject("topSchoolsScoreRadarJson", objectMapper.writeValueAsString(topSchoolsScoreRadar));

        // 10. 综合柱状图数据（学校调剂与分数线）
        List<YearlySchoolAdjustmentScore> yearlySchoolAdjustmentScore = kaoyanService.getYearlySchoolAdjustmentScore();
        view.addObject("yearlySchoolAdjustmentScoreJson", objectMapper.writeValueAsString(yearlySchoolAdjustmentScore));

        return view;


    }
}